<?php

namespace App\Services\Bankcard\Bankcard;

use App\Models\Bankcard\Bankcard;

/**
 * 银行卡逻辑层
 * Class BankcardLogic
 * @package App\Services\Bankcard\Bankcard
 */
class BankcardLogic
{
    /*后台 - 获取银行卡列表*/
    public function cardList($data)
    {
        if (isset($data['status']) && $data['status'] !== '') {
            $_where[] = ['status', '=', $data['status']];
        } else {
            $_where[] = ['status', '<>', -1];
        }
        $que = Bankcard::where($_where);

        if (isset($data['keywords']) && $data['keywords']) {
            $keywords = $data['keywords'];
            $res = $que->where(function ($query) use ($keywords) {
                $query ->where('user_id', 'like', "%{$keywords}%")
                       ->orWhere('account', 'like', "%{$keywords}%")
                       ->orWhere('name', 'like', "%{$keywords}%");
            })->paginate($data['listRows'], null, null, $data['page'])->toArray();
        } else {

            $res = $que->paginate($data['listRows'], null, null, $data['page'])
                ->toArray();
        }

        return auto_return_page($res);
    }

    /*我的/管理员 - 删除银行卡*/
    public function delCard($data): array
    {
        $_where[] =['id','=',$data['id']];
        isset($data['user_id']) && $_where[] = ['user_id','=',$data['user_id']];
        $_where[] = ['status','<>', -1];

        $res = Bankcard::where($_where)->update(['status'=>-1]);
        return auto_return($res, '成功删除银行卡', '删除银行卡失败');
    }

    /*后台 - 审核银行卡*/
    public function audit($data): array
    {
        $_where[] = ['id','=',$data['id']];
        $_where[] = ['status','=',0];

        $res = Bankcard::where($_where)->update(['status'=>$data['status']]);
        return auto_return($res);
    }
}